package com.apex.common.controller;

import com.apex.common.context.BaseContextHandler;
import com.apex.common.entity.SchoolInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2017/12/27
 */
public abstract class BaseController extends BaseContextHandler{

    @Autowired
    private SchoolInformation schoolInformation;

    public String getCurrentUserName(){
        return BaseContextHandler.getUsername();
    }

    public String getCurrentUserId(){
        return BaseContextHandler.getUserID();
    }

    public String getCurrentToken(){
        return BaseContextHandler.getToken();
    }

    /**
     * 获取登录用户账号
     * @param request
     * @return
     */
    public String getUserId(HttpServletRequest request){
        SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        if(null!=securityContextImpl){
            return securityContextImpl.getAuthentication().getName();
        }
        return "";
    }
    @ModelAttribute("schoolInformation")
    public SchoolInformation getTitle(){
        return schoolInformation;
    }

}
